package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.entity.SysCompany;
import com.nuoniu.sibanyun.entity.SysEmployee;
import com.nuoniu.sibanyun.entity.SysUser;
import com.nuoniu.sibanyun.entity.dto.SysRoleDto;
import com.nuoniu.sibanyun.entity.dto.SysUserDto;
import com.nuoniu.sibanyun.entity.vo.OfficeTreeData;
import com.nuoniu.sibanyun.entity.vo.OfficeUserVo;
import com.nuoniu.sibanyun.entity.vo.SysEmployeeSimpleVo;
import com.nuoniu.sibanyun.mapper.provider.SysUserMapperProvider;
import com.nuoniu.sibanyun.userThread.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@Component
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
//zy新增
    /**
     * 根据机构获取员工
     * @author 小啊刚
     * @date 2020/5/30
     */
    @Select("select concat(id, '_user') as id,real_name as name, #{id} as parentId from sys_user where office_id = #{id} and status = 0;")
    List<OfficeTreeData> getByOffice(Long id);
    /**
     * 修改员工姓名
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update sys_user set real_name = #{realName} where id = #{id}")
    void updateEmpName(Long id, String realName);
    /**
     * 根据用户id获取员工
     * @author 小啊刚
     * @date 2020/6/3
     */
    @Select("select * from sys_user where id = #{id}")
    SysUser getByUser(Long id);

    Page<SysEmployeeSimpleVo> pageSysEmployeeSimpleVo(Page page, Integer companyId, Integer officeId, String searchKey);

    @Select("select mobile from sys_user where company_id=#{companyId} and id is null")
    String[] getPhoneByCompanyId(Integer companyId);

    /**
     * 根据手机号查找员工
     * @author 小啊刚
     * @date 2021-07-28
     */
    @Select("select * from sys_user where mobile=#{userPhone}")
    SysUser getByUserPhone(String userPhone);

    /**
     * 删除用户zy改
     * @author 小啊刚
     * @date 2022-06-02
     */
    @Update("update `sys_user` set company_name=null , company_id = null , office_code = null where id=#{userId}")
    void deleteByUserId(Integer userId);


    //************************************************************

    List<String> listRolesString(@Param("userName") String userName);

    List<String> listPermissionsString(@Param("userName") String userName);

    /**
     * 查询用户
     * @author 小啊刚
     * @date 2020/6/11
     */
    @Select("SELECT * FROM sys_user WHERE user_name = #{userName}")
    SysUser findByUserName(String userName);

//    List<SysUser> getByOffice(Long id);

    UserInfo getUserInfo(@Param("userName")String userName);

    /**
     * 新增用户
     * @author 小啊刚
     * @date 2020/6/11
     */
    @Insert("INSERT INTO sys_user (user_name, password, real_name, mobile, email, remarks, create_by, company_id, company_name) VALUES (#{userName}, #{password}, " +
            "#{realName}, #{mobile}, #{email}, #{remarks}, #{createBy}, #{companyId}, #{companyName})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Long insertUser(SysUser user);

    Page<SysUserDto> getUserPage(Page page, @Param("dto") SysUserDto userDto);

    /**
     * 获取用户角色列表
     * @author 小啊刚
     * @date 2020/6/12
     */
    @Select("select u.role_id id, r.role_name roleName from sys_user_role u left join sys_role r on u.role_id = r.id where u.user_id = #{id}")
    List<SysRoleDto> getUserRole(Long id);

    @Select("select role_id FROM sys_user_role WHERE user_id = #{userId}")
    ArrayList<Integer> getUserRoleIds(Integer userId);

    /**
     * 查询用户邮箱
     * @author 小啊刚
     * @date 2020-07-16
     */
    @Select("select * from sys_user where email = #{email}")
    SysUser findByEmail(String email);

    /**
     * 检查邮箱
     * @author 小啊刚
     * @date 2020-07-16
     */
    @Select("select * from sys_user where email = #{email} and id != #{id}")
    SysUser checkUserEmail(SysUser user);

    /**
     * 获取公司用户列表
     * @author 小啊刚
     * @date 2020-07-22
     */
    @Select("select * from sys_user where company_id = #{companyId} and status = 0")
    List<UserInfo> getUserList(Integer companyId);

    @Select("SELECT id FROM sys_user where company_id = #{companyId} AND office_id IN (${officeIds})")
    List<String> listUserIdsByOfficIds(Integer companyId,String officeIds);

    /**
     * 获取管理员
     * @author 小啊刚
     * @date 2020-07-24
     */
    @Select("select u.* from sys_user u left join sys_user_role r on u.id = r.user_id where r.role_id = #{roleId} and u.company_id = #{companyId}")
    SysUser getSysManager(Integer companyId, Integer roleId);

    /**
     * 获取推荐用户
     * @author 小啊刚
     * @date 2020-07-27
     */
    @Select("select * from sys_user where user_name = #{sharePhone} and user_code = #{shareCode}")
    SysUser getShareUser(String shareCode, String sharePhone);

    /**
     * 获取下级会员列表
     * @author 小啊刚
     * @date 2020-07-28
     */
    @SelectProvider(type = SysUserMapperProvider.class, method = "getShareUserList")
    Page<SysUser> getShareUserList(Page page, SysUser user);

    /**
     * 获取下级公司会员
     * @author 小啊刚
     * @date 2020-07-29
     */
    //@SelectProvider(type = SysUserMapperProvider.class, method = "getShareCrmList")
    Page<Map> getShareCrmList(Page page, @Param("company") SysCompany company, @Param("index") Integer index);

    /**
     * 停用用户
     * @author 小啊刚
     * @date 2020-08-03
     */
    @Update("update sys_user set status = 2 where id in (select * from (select id from sys_user where company_id = #{companyId} and id !=#{userId} and status = 0 limit ${freeUsers},${size}) t)")
    void updateStatus(Long companyId, int size, Integer freeUsers, Long userId);

    /**
     * 修改公司名称
     * @author 小啊刚
     * @date 2020-08-04
     */
    @Update("update sys_user set company_name = #{companyName} where company_id = #{id}")
    void updateCompanyName(SysCompany company);

    List<OfficeUserVo> getDataScopeOfficeUsers(List<String> officeIds, List<String> userIds);

    List<OfficeUserVo> getAllCompanyUsers(Integer companyId);

    /**
     * 获取部门下 所有用户的Id
     * @param officeId
     * @return
     */
    List<Integer> getOfficeAllUserIds(Integer companyId,Integer officeId);

    /**
     * 个人租户
     * @author 小啊刚
     * @date 2020-08-25
     */
    Page<SysUser> pageMember(Page page, @Param("user") SysUser user);

    /**
     * 获取用户列表
     * @author 小啊刚
     * @date 2021-09-28
     */
    Page<SysUserDto> getPageList(Page page, @Param("user") SysUserDto userDto);

    /**
     * 根据标识获取菜单
     * @author 小啊刚
     * @date 2022-01-08
     */
    @Select("select su.id as userId,su.real_name from sys_user su left join sys_user_role ur on su.id=ur.user_id where su.company_id = #{companyId} and ur.role_id in " +
            "(select r.id from sys_role r right join sys_role_menu rm on r.id=rm.role_id where menu_id = #{menuId} and (r.company_id=#{companyId} or r.company_id=1))")
    List<UserInfo> getByMenuPermissionList(Integer companyId, Long menuId);

    /**
     * 获取我的所有推客
     * @author 小啊刚
     * @date 2022-05-23
     */
    List<SysUser> getShareUserAll(Integer userId);

    /**
     * 删除机构所有数据
     * @author 小啊刚
     * @date 2022-06-01
     */
    @Delete("delete from ${formNameEn} where company_id=#{companyId}")
    void deleteFormCompany(String formNameEn, Integer companyId);

    /**
     * 删除机构身份
     * @author 小啊刚
     * @date 2022-06-02
     */
    @Delete("update sys_user set company_id=null, company_name='' where id=#{userId}")
    void deleteCompany(Integer userId);
}
